import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ConfigProvider} from "../../providers/config/config";
import {ValidateProvider} from "../../providers/validate/validate";
import {UtilsProvider} from "../../providers/utils/utils";
import {StorageProvider} from "../../providers/storage/storage";

@IonicPage({
    segment: 'service/talent_pool/search',
    defaultHistory: ['ServicePage']
})
@Component({
    selector: 'page-talent-pool-search',
    templateUrl: 'talent-pool-search.html',
})
export class TalentPoolSearchPage {
    options = [];
    condition = {};
    isShow=0;
    searchType:number = 1;
    searchOptions = {};
    searchNavId:number = 37;
    page=1;
    resumeList=[];
    resumeTotal=[];
    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public config: ConfigProvider,
        public validate: ValidateProvider,
        public util: UtilsProvider,
        public storage: StorageProvider
    ) {
        // if (!this.validate.checkLogin()) {
        //     this.navCtrl.push('LoginPage');
        // }
    }

    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    ionViewDidLoad() {
        this.getResume();
        
    }

    getResume() {
        //获取简历选项
        let apiUrl_options = "/user_info/resume_option";
        this.httpService.requestData(apiUrl_options, response => {
            if (response['error'] === 200) {
                this.options['cuisine'] = response['data']['cuisine'];
                // this.options['job_type'] = response['data']['job_type'];
                this.options['target_japanese'] = response['data']['target_japanese'];
                this.options['target_western'] = response['data']['target_western'];
                this.options['target_work'] = response['data']['target_work'];
                this.options['working_age'] = response['data']['working_age'];
                this.options['areas'] = response['data']['area'];
            }
        });
        this.getResumeList(null);
    }

    select(name,data){
        if(!Array.isArray(this.condition[name])){
            this.condition[name] = [];
        }
        let index = this.util.inArray(this.condition[name],data)
        if(index > -1){
            this.condition[name].splice(index,1);
        }else{
            this.condition[name].push(data);
        }
    }

    debug(){
    }
    doInfinite(infiniteScroll) {
        this.page++;
        this.getResumeList(infiniteScroll);        
        
    }
    getResumeList(infiniteScroll){
        let postData = {
            token: this.config.getToken(),
            page: this.page
        };
        if(this.storage.get('resumeSearchData')){
            Object.assign(postData,JSON.parse(this.storage.get('resumeSearchData')));
        }
        let apiUrl = "/talents/resume_list";
        this.httpService.requestPostData(apiUrl, postData, (response)=>{
            if(response.error == 200) {                
                this.resumeTotal = response['data']['total'];
                if (infiniteScroll) {
                    this.resumeList = this.resumeList.concat(response['data']['lists']);
                    infiniteScroll.complete();
                    /*请求完成数据更新页面*/
                    if (response['data']['lists'].length < response['data']['page_size']) {   /*没有数据 禁用上拉更新*/
                        infiniteScroll.enable(false);
                    }
                }else{
                    this.resumeList = response['data']['lists'];
                }
            }
        })
    }
    /**
     * 选择不限时处理
     * */

    // selAll(type,options){
    //     let optionsArr = [];
    //     if(type == 'area'){
    //         for (let key in options) {
    //             optionsArr.push(options[key]['list_name']);
    //         }
    //     }else{
    //         for (let key in options) {
    //             optionsArr.push(options[key]);
    //         }
    //     }
    //     let temp = {};
    //     temp[type] = optionsArr;
    //     Object.assign(this.searchOptions, temp);
    // }

    submit(){
        this.searchOptions = {};

        for(let key in this.condition){
            let temp = {};
            let optionsArr = [];
            if(key == 'cuisine'){
                if(this.condition['cuisine'] == '不限'){
                    for (let k in this.options['cuisine']) {
                        optionsArr.push(this.options['cuisine'][k]);
                    }
                }else{
                    for (let k in this.condition['cuisine']) {
                        optionsArr.push(this.condition['cuisine'][k]);
                    }
                }
                temp['cuisine'] = optionsArr;
            }else if(key == 'target_work'){
                if(this.condition['target_work'] == '不限'){
                    for (let k in this.options['target_work']) {
                        optionsArr.push(this.options['target_work'][k]);
                    }
                }else{
                    for (let k in this.condition['target_work']) {
                        optionsArr.push(this.condition['target_work'][k]);
                    }
                }
                temp['target_work'] = optionsArr;
            }else{
                temp[key] = this.condition[key];
            }

            Object.assign(this.searchOptions, temp);
        }
        this.storage.set('resumeSearchData',JSON.stringify(this.searchOptions));
        this.page=1;
        this.getResumeList(null)        
        this.isShow=0;
        this.condition = {};
    }
    //禁止冒泡
    alert(e){
        e.stopPropagation()
    }
    reset(status){
        switch (status){
            case 1:
            this.condition['target_work'] = [];
            break;

            case 2:
            this.condition['cuisine'] = [];
            break;

            case 3:
            this.condition['money'] = '';
            break;

            case 4:
            this.condition['age'] = '';
            this.condition['sex'] = '';
            this.condition['working_age'] = '';
            break;

        };
    }
    resumeDetail(resume_id){
        // this.httpService.requestPostData("/user_info/check_level",{
        //     token: this.config.getToken()
        // }, (response)=>{
        //     if(response['error'] == 200 && response['reg_step'] == 2){
        //         //高级用户处理
        //         //查询是否为收费信息，是否已付费
        //         this.httpService.requestPostData("/talents/resume_detail", {
        //             token: this.config.getToken(),
        //             resume_id: resume_id
        //         }, data => {
                    // if (data['error'] === 300) {
                    //     this.util.presentConfirm(data['message'], ()=>{
                    //         this.httpService.requestPostData('/talents/resume_pay', {
                    //             token: this.config.getToken(),
                    //             resume_id: resume_id
                    //         }, res=>{
                    //             if(res['error'] == 200){
                    //                 this.navCtrl.push('ResumeDetailPage', {resume_id: resume_id});
                    //                 return false;
                    //             }else{
                    //                 this.util.presentAlert(res['message']);
                    //                 return false;
                    //             }
                    //         })
                    //     });
                    //     return false;
                    // } else if(data['error'] == 200){
                    //     this.navCtrl.push('ResumeDetailPage', {resume_id: resume_id});
                    //     return false;
                    // } else {
                    //     this.util.presentAlert(data['message']);
                    // }
                    // if(data['error'] == 200){
                                        this.navCtrl.push('ResumeDetailPage', {resume_id: resume_id});
                                    // }else{
                                    //     this.util.presentAlert(data['message']);
        //                             }
        //         });
        //     }else{
        //         this.util.presentConfirm('该功能为高级用户权限，现在去完善注册信息？', ()=>{
        //             this.navCtrl.push('RegisterPage');
        //         });
        //     }
        // });

    }
}
